const express =require('express');

// const data = require('./data/newdata.json')

const newsModel = require('./08-model');

const app = express();

app.listen(80,()=>{
    console.log("启动成功");
})

app.get('/list',(req,res)=>{
    newsModel.find((err,data)=>{
        res.render('list.ejs',{data});
    })
})

app.get('/detail/:id',(req,res)=>{
    let {id} = req.params;
    newsModel.findOne({_id:id},(err,result)=>{
        res.render('detail.ejs',{result});
    })
})

app.use(express.urlencoded({extended:false}));

app.post('/postNews',(req,res)=>{
    // 任务 获取请求体数据 并且把数据存入到数据库

    // console.log(req.body);

    newsModel.create(req.body,(err,data)=>{
        console.log(err);
    })


    res.send('ok');
});